package com.encryption;

import java.io.FileOutputStream;

import com.lowagie.text.pdf.PdfEncryptor;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;

public class EncryptionTest {
	public static void main(String[] args) throws Exception {
		PdfReader reader = new PdfReader("D:/temp/upload_temp/upload/2012/05/28/test.pdf");
		PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
				"D:/temp/upload_temp/upload/2012/05/28/Encrypted1.pdf"));
		stamper.close();

		getEncryptionInformation("D:/temp/upload_temp/upload/2012/05/28/test.pdf", null);
		getEncryptionInformation("D:/temp/upload_temp/upload/2012/05/28/Encrypted1.pdf", "World");
	}

	public static void getEncryptionInformation(String filename,
			String ownerpassword) throws Exception {
		PdfReader reader;
		if (ownerpassword == null)
			reader = new PdfReader(filename);
		else
			reader = new PdfReader(filename, ownerpassword.getBytes());
		System.out.println("Encrypted? " + reader.isEncrypted());
		if (reader.isEncrypted()) {
			System.out.println("Permissions: "
					+ PdfEncryptor.getPermissionsVerbose(reader
							.getPermissions()));
			System.out.println("128 bit? " + reader.is128Key());
		}
	}
}
